﻿using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using SubStationManagement.Server.Data;
using SubStationManagement.Server.Models;
using SubStationManagement.Server.Services;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;

namespace SubStationManagement.Server.Components.Pages
{
    /// <summary>
    /// 表格编辑示例
    /// </summary>
    public partial class DIDOModuleList
    {
        private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

        protected override Task OnInitializedAsync()
        {
            base.OnInitialized();
            StateHasChanged();
            return Task.CompletedTask;
        }

        private async Task<QueryData<DIDOModule>> OnQueryAsync(QueryPageOptions options)
        {
            List<DIDOModule> items = await dIDOModuleService.GetAsync();
            var total = items.Count();
            items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();
            return new QueryData<DIDOModule>()
            {
                Items = items,
                TotalCount = total,
                IsSorted = true,
                IsFiltered = options.Filters.Any(),
                IsSearch = options.Searches.Any(),
                IsAdvanceSearch = options.AdvanceSearches.Any()
            };
        }

        private async Task<bool> OnSaveAsync(DIDOModule DIDOModule, ItemChangedType changedType)
        {
            if (changedType == ItemChangedType.Add)
            {
                await dIDOModuleService.CreateAsync(DIDOModule);
            }
            else
            {
                await dIDOModuleService.UpdateAsync(DIDOModule.Id, DIDOModule);
            }
            return await Task.FromResult(true);
        }

        private async Task<bool> OnDeleteAsync(IEnumerable<DIDOModule> DIDOModules)
        {
            foreach (var item in DIDOModules)
            {
                await dIDOModuleService.RemoveAsync(item.Id);
            }
            return await Task.FromResult(true);
        }
    }
}
